/*
	FileName : JT_C_REC_Item.c
	Description :
		Manage the format of JT_C STS record item.
		Manage the buffer that saving the received STS items, 
		and JT_C working status according that.
	
	Created on 2017.11.02 by WFL.

	Revision History:

*/


/* Includes ------------------------------------------------------------------*/



#include "JT_C_REC_Item.h"
#include "MiscFnx.h"		/// for CRC function
#include "GF_WFL_PI.h"

JT_C_STATUS JTC_sts;

//-----------------------------------------------------------------------
uint8_t JT_C_REC_ITEM_getMainUse(uint8_t *itemStartAddr)
{
	return itemStartAddr[JT_C_REC_ITEM_MAINUSE];
}
uint8_t JT_C_REC_ITEM_getSD(uint8_t *itemStartAddr)
{
	return (itemStartAddr[JT_C_REC_ITEM_MAINUSE]&JT_C_REC_ITEM_MAINUSE_b_SD0_SD2);
}
uint8_t JT_C_REC_ITEM_getCoilErr(uint8_t *itemStartAddr)
{
	return itemStartAddr[JT_C_REC_ITEM_COILERR];
}
uint8_t JT_C_REC_ITEM_getBoardVid(uint8_t *itemStartAddr)
{
	return itemStartAddr[JT_C_REC_ITEM_BOARDVID];
}
uint8_t JT_C_REC_ITEM_if_RTC_Valid(uint8_t *itemStartAddr)
{
	if(JT_C_REC_ITEM_getBoardVid(itemStartAddr) & JT_C_REC_ITEM_BOARDVID_b_RTC)
		return 1;
	return 0;
}
uint8_t JT_C_REC_ITEM_if_CTRL_Valid(uint8_t *itemStartAddr)
{
	uint8_t tmp;
	tmp = JT_C_REC_ITEM_getBoardVid(itemStartAddr) & JT_C_REC_ITEM_BOARDVID_b_CTRL;
	if(tmp == JT_C_REC_ITEM_BOARDVID_b_CTRL)
		return 1;
	return 0;
}

uint8_t JT_C_REC_ITEM_JYJChanged(uint8_t *itemStartAddr)
{
	if (itemStartAddr[JT_C_REC_ITEM_JYJ] > JT_C_REC_ITEM_SAMPLE_50V_HIGH)
		JTC_sts.JYJ = 1;
	else
		JTC_sts.JYJ = 0;
	if (JTC_sts.JYJ == JTC_sts.lastJYJ){
		
		if(JTC_sts.JYJSameCnt != 0xFF)
			JTC_sts.JYJSameCnt++;
		if (JTC_sts.JYJSameCnt >= 2)
			JTC_sts.JYJChanged = 0;
	}
	else{
		
		JTC_sts.JYJSameCnt = 0;
		JTC_sts.JYJChanged = 1;
	}

	if(JTC_sts.JYJChanged)
		return 1;
	return 0;
}
uint32_t JT_C_REC_ITEM_getTAX39time(uint8_t *itemStartAddr)
{
	return Mem_LE_Get32(itemStartAddr, JT_C_REC_ITEM_TAX39TIME);
}
uint8_t JT_C_REC_ITEM_isDispath(uint8_t *itemStartAddr)
{
	if(itemStartAddr[JT_C_REC_ITEM_TAX39HOME] & JT_C_REC_ITEM_TAX39HOME_b_dispatch)
		return 1;
	return 0;
}
uint8_t JT_C_REC_ITEM_notMonitoring(uint8_t *itemStartAddr)
{
	if(itemStartAddr[JT_C_REC_ITEM_TAX39HOME] & JT_C_REC_ITEM_TAX39HOME_b_notmonitor)
		return 1;
	return 0;
}
uint32_t JT_C_REC_ITEM_getSpeed(uint8_t *itemStartAddr)
{
	uint32_t returnValue = 0;
	returnValue += itemStartAddr[JT_C_REC_ITEM_TAX39SPEED + 2];
	returnValue <<= 8;
	returnValue += itemStartAddr[JT_C_REC_ITEM_TAX39SPEED + 1];
	returnValue <<= 8;
	returnValue += itemStartAddr[JT_C_REC_ITEM_TAX39SPEED + 0];
	
	return 	returnValue;
}

uint32_t JT_C_REC_ITEM_getJCH_NO(uint8_t *itemStartAddr)
{
	return Mem_LE_Get32(itemStartAddr, JT_C_REC_ITEM_TAX39JCH);
}

uint16_t JT_C_REC_ITEM_getJCtype(uint8_t *itemStartAddr)
{
	uint16_t jctype = 0;
	jctype += itemStartAddr[JT_C_REC_ITEM_TAX38EXJCTYPE];
	jctype <<= 8;
	jctype += itemStartAddr[JT_C_REC_ITEM_TAX39JCTYPE];
	return jctype;
}
//---------------------------------------------------------------------------------

#if (STS_FRAME_USE_EXTERNAL_MEM==0)
__ALIGN_BEGIN		STS_FRAME StsFrame[STS_BUF_SIZE];		__ALIGN_END;
#endif


void STS_FRAME_copyIn(STS_FRAME *sts_f, uint8_t *itemStartAddr)
{
	uint16_t i;
	uint8_t *ptr;
	ptr = (uint8_t *)(sts_f);
	for (i = 0; i < STS_FRAME_SIZE; i++)
		ptr[i] = itemStartAddr[i];
	return;
}
void STS_FRAME_recPartCopyOut(STS_FRAME *sts_f, uint8_t *readBuf)
{
	uint16_t i;
	uint8_t *ptr;
	ptr = (uint8_t *)sts_f;
	
	for (i = 0; i < STATUS_DATA_COUNT; i++)
		readBuf[i] = ptr[i];
	return;
}

void STS_FRAME_readNextUnhandledFrame(STS_FRAME *sts_f_array, 
										uint8_t *readBuf, 
										JT_C_STATUS *jtcsts,
										uint8_t frameNumber)
{
	uint16_t k,j,i;
	
	for (j = 0; j < frameNumber; j++){
		
		if (jtcsts->stsHandledCnt >= jtcsts->stsReceiveCnt){
			
			for (i = 0; i < STATUS_DATA_COUNT; i++)
				*readBuf++ = 0;
		}
		else{
			
			k = jtcsts->stsHandledCnt & STS_BUF_MASK;
			STS_FRAME_recPartCopyOut(&(sts_f_array[k]),readBuf);
			readBuf += STATUS_DATA_COUNT;
			jtcsts->stsHandledCnt++;
		}
	}
	return;
}

void STS_FRAME_setRecTypeAndReCrc(STS_FRAME *sts_f, uint8_t recType)
{
	uint8_t *ptr;
	
	ptr = (uint8_t*)(sts_f);
	
	Mem_LE_Set8(ptr,JT_C_REC_ITEM_RECTYPE,recType);
	STS_FRAME_reCrc(sts_f);
	
	return;
}

void STS_FRAME_reCrc(STS_FRAME *sts_f)
{
	uint16_t crc;
	uint8_t *ptr;
	
	ptr = (uint8_t*)(sts_f);
	
	crc = Calc_CRCByte(ptr, STATUS_DATA_COUNT - 2);
	Mem_LE_Set16(ptr,JT_C_REC_ITEM_CRC_L,crc);
	return;
}

void STS_FRAME_setStatusOfREC_CPU(STS_FRAME *sts_f,JT_C_STATUS *jtc,uint8_t sw)
{
	//check "JE" signal
	if ((sts_f->CtrlABuf[16] & 0x02) || \
		(sts_f->CtrlBBuf[16] & 0x02))
		sts_f->Led = 0x00;///led off
		
	if ((sts_f->V110[9] > 60) && (sts_f->V110[11] > 60)){
		
		sts_f->SXX |= 0x20;///I/II both high
	}
	
	if ((sts_f->SX > 35*4) && (sts_f->XX > 35*4)){
		
		sts_f->SXX |= 0x40;
	}
	
	if ((sts_f->SX < 10*4) && (sts_f->XX < 10*4)){
		jtc->SXXSilenceCnt++;
		if (jtc->SXXSilenceCnt > 2)
			sts_f->SXX &= 0xfc;
	}
	else{
		jtc->SXXSilenceCnt = 0;
	}
	
	if (jtc->trainAtHome)
		sts_f->Misc |= 0x80;
				
	sts_f->rsvd1 = sw;
	
	return;
}
//----------------------------------------------------------------------------------
void JT_C_STATUS_init(JT_C_STATUS *jtc)
{
	jtc->stsTime = 0;
	jtc->lastStsTime = 0xFF;
	jtc->lastOrder = 0xFF;
	
	jtc->stsReceiveCnt = 0;
	jtc->stsHandledCnt = 0;
	jtc->isFirstSts = 1;
	
	jtc->currLed = 0;
	jtc->lastLed = 0xFF;
	jtc->sameLedCnt = 0;
	jtc->noCodeCnt = 0;
	jtc->currSD = 0;
	jtc->lastSD = 0xFF;
	jtc->ledChangeCntInSameJYJ = 0;
	jtc->trainAtHome = 0;
	
	jtc->ledNoCode = 0;
	jtc->lastLedNoCode = 0;
	jtc->JYJChanged = 0;
	jtc->JYJSameCnt = 0;
	jtc->JYJ = 0;
	jtc->lastJYJ = 0;
	jtc->JCH_NO = 0;
	jtc->JC_type = 0;
	jtc->coilStatus = 0;
	jtc->SXXSilenceCnt = 0;
	return;
}

void JT_C_STATUS_updateAllLastStatus(JT_C_STATUS *jtc, uint8_t *itemStartAddr)
{
	jtc->lastLed = jtc->currLed;
	jtc->lastSD = jtc->currSD;
	jtc->lastStsTime = jtc->stsTime;	
	jtc->lastOrder = Mem_LE_Get8(itemStartAddr,JT_C_REC_ITEM_ORDER);	
	jtc->lastLedNoCode = jtc->ledNoCode;
	jtc->lastJYJ = jtc->JYJ;
}







///EOF
